package com.server.server;

import java.util.ArrayList;

import org.apache.mina.core.session.IoSession;

import com.server.app.PlayerManager;
import com.server.app.RoomManager;
import com.server.object.Player;
import com.server.object.PlayerData;
import com.server.protocols.SProtocols;
import com.server.request.MatchRoomReq;
import com.server.response.MatchRoomResp;
import com.server.status.SocketState;
import com.webserver.server.net.BaseMessage;
import com.webserver.server.net.BaseServer;

public class MatchRoomServer implements BaseServer {
	public int GetProtocol() {
		return SProtocols.MATCH_ROOM;
	}

	public BaseMessage GetRequest() {
		return new MatchRoomReq();
	}

	public BaseMessage handle(IoSession is,BaseMessage request) {
		MatchRoomReq req = (MatchRoomReq)request;
		MatchRoomResp resp;
		Player player = PlayerManager.GetInstance().GetPlayer(req.getUserInfo().uid);
		if(player != null) {
			if(player.room == null) {
				String room_id = RoomManager.GetInstance().MatchRoom(player.appid, req.room_tag, req.player_count, req.getUserInfo().uid);
				if(room_id != null) {
					resp = new MatchRoomResp(SocketState.SUCCESSED.ordinal(), room_id, player.room.GetPlayerData(req.getUserInfo().uid), player.room.master_id);
				}else {
					resp = new MatchRoomResp(SocketState.FAILED.ordinal(), "", new ArrayList<PlayerData>(), 0);
				}
			}else {
				resp = new MatchRoomResp(SocketState.INROOM.ordinal(), "", new ArrayList<PlayerData>(), 0);
			}
		}else {
			resp = new MatchRoomResp(SocketState.RELOGIN.ordinal(), "", new ArrayList<PlayerData>(), 0);
		}
		return resp;
	}
}